﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using KPIS.Utils;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using _WebShared.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_TimeAttendants : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IAbsenceTypeBLL appBLL = new AbsenceTypeBLL();
        private TimeAttendantsBLL TimeAtt = new TimeAttendantsBLL();

        private const int ItemsPerRequest = 10;
        private object _dataItem = null;

        //Initializing the new variable updating ViewState data.
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnLoad(System.EventArgs e)
        {
            if (DataItem != null)
            {
                bindAbsenceType();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSeq.Text = DataBinder.Eval(DataItem, "WORKING_SEQ").ToString();
                    cmbEmployee.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EMP_NAME").ToString(), DataBinder.Eval(DataItem, "EMP_SEQ").ToString()));
                    cmbEmployee.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "EMP_SEQ"));
                    cmbWorkingStatus.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "WORKING_STATUS"));
                    txtWorkingDate.SetValue(DataBinder.Eval(DataItem, "WORKING_DATE", "{0:dd/MM/yyyy}"));
                    txtWorkingStart.Text = DataBinder.Eval(DataItem, "WORKING_START").ToString();
                    txtWorkingEnd.Text = DataBinder.Eval(DataItem, "WORKING_END").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "REMARKS").ToString();

                }
                else
                {

                }
                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        

        private void bindAbsenceType()
        {
            IList<AbsenceTypeInfo> infoList = appBLL.ListAbsenceTypes();

            ddlType.DataSource = infoList;
            ddlType.DataBind();
        }

        protected void cmbEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbEmployee.Items.Clear();


            DataSet ds = empBLL.LoadEmployees((int?)null
                , Session["EMPTYPE"].ToString()
                , (int?)null
                , Int32.Parse(Session["WORKING"].ToString())
                , true, e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["FULL_NAME"].ToString(), dt.Rows[i]["EMP_SEQ"].ToString()));

                TableRow tr = new TableRow();

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["FULL_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(150);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["POS_TEXT"].ToString();
                tdClassSpecName.Width = Unit.Pixel(100);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["CLASS_NAME"].ToString();
                tdJobName.Width = Unit.Pixel(100);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                ((Table)cmbEmployee.Items[j].FindControl("tableEmployee")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            int chkError = TimeAtt.Insert(txtWorkingDate.GetStringValue()
                , Int32.Parse(cmbEmployee.SelectedValue)
                , txtWorkingStart.TextWithLiterals
                , txtWorkingEnd.TextWithLiterals
                , cmbWorkingStatus.SelectedValue
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int chkError = TimeAtt.Update(Int32.Parse(lblSeq.Text)
                , txtWorkingDate.GetStringValue()
                , Int32.Parse(cmbEmployee.SelectedValue)
                , txtWorkingStart.TextWithLiterals
                , txtWorkingEnd.TextWithLiterals
                , cmbWorkingStatus.SelectedValue
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }



        protected void cmbWorkingStatus_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cmbWorkingStatus.SelectedValue == "A")
            {
                ddlType.Visible = true;
            }
            else
            {
                ddlType.Visible = false;
            }
        }
    }
}